<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('抄表')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>电费批次：</label>
                                <input autocomplete="off" type="text" id="etldt" placeholder="请填写批次"  name="etldt" required/>
                            </li>
                            <li>
                                <label>楼层：</label>
                                <select name="floorName" class="form-control" id="floorName" required>
                                    <option value="">请选择</option>
                                    <option th:each="floorInfo:${floorInfos}" th:text="${floorInfo.floorName}" th:value="${floorInfo.floorName}"></option>
                                </select>
                            </li>
                            <li>
                                <label>抄表日期：</label>
                                <input autocomplete="off" type="text" class="time-input" id="issueDate" placeholder="请填写日期"  name="issueDate" required/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="query()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="sujian:elecMeter:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="sujian:elecMeter:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="sujian:elecMeter:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-success" onclick="importExcel()" shiro:hasPermission="sujian:elecMeter:import">
                    <i class="fa fa-upload"></i> 导入
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
     <th:block th:include="include :: footer" />
     <th:block th:include="include :: bootstrap-fileinput-js" />
     <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var options;
        var prefix = ctx + "sujian/elecMeter";

        $(function () {
            initEtldt();
        })


        function query() {
            if(options!=null&&options!=undefined){
                var params = $("#bootstrap-table").bootstrapTable('getOptions');
                $("#bootstrap-table").bootstrapTable('refresh', params);
            }else{
                options = {
                    url: prefix + "/list",
                    createUrl: prefix + "/add",
                    updateUrl: prefix + "/edit/{id}",
                    removeUrl: prefix + "/remove",
                    importUrl: prefix + "/import",
                    importTemplateUrl: prefix + "/importTemplate",
                    uniqueId:"ammeterId",
                    modalName: "抄表信息",
                    columns: [{
                        checkbox: true
                    },
                        {
                            field: 'ammeterId',
                            title: '抄表编号',
                            visible: false
                        },
                        {
                            field: 'floorName',
                            title: '楼层'
                        },
                        {
                            field: 'issueDate',
                            title: '本期抄表时间',
                            // sortable: true
                        },
                        {
                            field: 'degreePreA',
                            title: 'A表上期度数'
                        },
                        {
                            field: 'degreeIssueA',
                            title: 'A表本期度数'
                        },
                        {
                            field: 'degreeA',
                            title: 'A表用电量',
                            // sortable: true
                        },
                        {
                            field: 'degreePreB',
                            title: 'B表上期度数'
                        },
                        {
                            field: 'degreeIssueB',
                            title: 'B表本期度数'
                        },
                        {
                            field: 'degreeB',
                            title: 'B表用电量',
                            // sortable: true
                        },
                        {
                            field: 'degreePreC',
                            title: 'C表上期度数'
                        },
                        {
                            field: 'degreeIssueC',
                            title: 'C表本期度数'
                        },
                        {
                            field: 'degreeC',
                            title: 'C表用电量',
                            // sortable: true
                        },
                        {
                            field: 'degreePreD',
                            title: 'D表上期度数'
                        },
                        {
                            field: 'degreeIssueD',
                            title: 'D表本期度数'
                        },
                        {
                            field: 'degreeD',
                            title: 'D表用电量',
                            // sortable: true
                        },
                        {
                            field: 'degreePre',
                            title: '上期度数',
                            visible: false
                        },
                        {
                            field: 'degreeIssue',
                            title: '本期度数'
                        },
                        {
                            field: 'degree',
                            title: '总用电量',
                            // sortable: true
                        },
                        {
                            field: 'status',
                            title: '状态',
                            formatter: function (value,row,index) {
                                return statusTools(row);
                            }
                        },
                        {
                            field: 'remark',
                            title: '备注',
                            visible: false
                        }]
                };
                $.table.init(options);
            }

        }

        //状态展示
        function statusTools(row){
            if(row.status == 0){
                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\''+row.ammeterId+'\')"></i>'
            }else{
                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\''+row.ammeterId+'\')"></i>'
            }
        }

        //状态 停用
        function disable(ammeterId) {
            $.modal.confirm("确认要停用电表吗？",function() {
                $.operate.post(prefix+"/changeStatus",{"ammeterId":ammeterId,"status":0})
            })
        }
        //状态 启用
        function enable(ammeterId) {
            $.modal.confirm("确认要启用电表吗？",function() {
                $.operate.post(prefix+"/changeStatus",{"ammeterId":ammeterId,"status":1})
            })
        }
        function importExcel(formId, width, height) {
            table.set();
            layer.open({
                type: 1,
                area: ['400px','230px'],
                fix: false,
                //不固定
                maxmin: true,
                shade: 0.3,
                title: '导入' + table.options.modalName + '数据',
                content: $('#importTpl').html(),
                btn: ['<i class="fa fa-check"></i> 导入', '<i class="fa fa-remove"></i> 取消'],
                // 弹层外区域关闭
                shadeClose: true,
                btn1: function(index, layero){
                    var file = layero.find('#elecFile').val();
                    if (file == '' || (!$.common.endWith(file, '.xls') && !$.common.endWith(file, '.xlsx'))){
                        $.modal.msgWarning("请选择后缀为 “xls”或“xlsx”的文件。");
                        return false;
                    }
                    var index = layer.load(2, {shade: false});
                    $.modal.disable();
                    var formData = new FormData(layero.find('form')[0]);
                    $.ajax({
                        url:prefix + "/importExcel",
                        data: formData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        type: 'POST',
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $.modal.closeAll();
                                $.modal.alertWarning(result.msg);
                                $.table.refresh();
                            } else {
                                layer.close(index);
                                $.modal.enable();
                                $.modal.alertError(result.msg);
                            }
                        }
                    });
                }
            });
        }

        // function initEtldt() {
        //     var date = new Date();
        //     var year = date.getFullYear();
        //     var month = date.getMonth() + 1;
        //     if(month == 0){
        //         month = 12;
        //         year = year - 1;
        //     }
        //     if(month < 10){
        //         month = "0" + month;
        //     }
        //     $('#etldt').val(""+year+month);
        // }
    </script>
</body>
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="elecFile" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果数据已经存在，更新这条数据。"> 是否更新已经存在的数据
                &nbsp;	<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs" shiro:hasPermission="sujian:elecMeter:template"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
                提示：仅允许导入“xls”或“xlsx”格式文件！！！
            </font>
        </div>
    </form>
</script>

</html>